#include "String.h"
using std::string;
namespace zg_util{
    /*替换字符串中所有子串*/
    std::string String::ReplaceAll(const std::string &str,std::string oldstr,std::string new_str){
        std::string replaced_str=str;
        std::size_t index=replaced_str.find(oldstr);
        while(index!=std::string::npos){
            replaced_str.replace(index,oldstr.size(),new_str);
            index=replaced_str.find(oldstr);
        }
        return replaced_str;
    }

    void String::SeperateStringToVector(const std::string& str,std::vector<std::string>& vec,const std::string &sign){
        if(sign.size()!=1){
            return;
        }
        if(str.size()==0)
            return;
        std::string raw=str;
        size_t index=raw.find(sign);
        while(index!=std::string::npos){
            if(index!=raw.size()-1){
                vec.push_back(raw.substr(0,index));
                raw=raw.substr(index+1,raw.size()-index-1);
            }else{
                raw.clear();
                break;
            }
            index=raw.find(sign);
        }
        if(raw.size()!=0)
            vec.push_back(raw);
    }
    void String::SeperateStringToSet(const std::string& str,std::set<std::string>& vset,const std::string &sign){
        if(sign.size()!=1){
            return;
        }
        if(str.size()==0)
            return;
        std::string raw=str;
        size_t index=raw.find(sign);
        while(index!=std::string::npos){
            if(index!=raw.size()-1){
                vset.insert(raw.substr(0,index));
                raw=raw.substr(index+1,raw.size()-index-1);
            }else{
                raw.clear();
                break;
            }
            index=raw.find(sign);
        }
        if(raw.size()!=0)
            vset.insert(raw);
    }
    std::string String::DiscardSpace(const std::string& str){
        return ReplaceAll(str," ","");
    }

}